package com.jf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf.blog.bean.Comment;
import com.jf.blog.form.comment.CommentForm;
import com.jf.blog.mapper.CommentMapper;
import com.jf.blog.service.CommentService;
import com.jf.blog.util.ResultJson;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author JF
 * @since 2025-06-20
 */
@Service
@Transactional
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Override
    public void save(CommentForm form) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(form, comment);
        comment.setCommentTime(new Date());
        this.save(comment);
    }

    @Override
    public ResultJson getPage(int page, int limit, int blogId) {
        IPage<Comment> iPage = this.page(new Page<>(page, limit), new QueryWrapper<Comment>().eq("blog_id", blogId));
        return ResultJson.success(iPage);
    }

    @Override
    public void status(int commentId) {
        Comment comment = this.getById(commentId);
        int status = comment.getStatus() == 1 ? 2 : 1;
        comment = new Comment();
        comment.setCommentId(commentId);
        comment.setStatus(status);
        this.updateById(comment);
    }

}
